<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Author extends CI_Controller {
    
    private $data = array();

    public function  __construct() {
        parent::__construct();

        //check session
        if( !$this->session->userdata('isLoggedIn') ) {
            redirect(base_url().'admin', 'refresh');
        }

        //Load Helper and library
        $this->load->helper("url");
        $this->load->library("ci_smarty");
        $this->load->helper("text"); 
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');
        $this->load->library('pagination'); 

        //Load Model
        $this->load->Model("admin/Mauthor");

        $this->data['nameAdmin'] = $this->session->userdata('name');

        $this->data['main_selected_cate'] = 'author';
    }

    public function listAuthor()
    {
        $this->data['listAuthor'] = $this->Mauthor->getAuthor();
        $this->ci_smarty->view('backend/list-author.tpl',$this->data); 
    }

    public function addAuthor()
    {
        if($this->input->post()){
            $data = $this->input->post();
            $this->Mauthor->insertAuthor($data); 
            redirect(base_url()."admin/list-author.html"); 
        }
    }

    public function updateAuthor($author_id)
    {
        $this->data['author'] = $this->Mauthor->getAuthorByID($author_id); 
        if($this->input->post()){
            $data = $this->input->post();
            $this->Mauthor->updateAuthor($data); 
            redirect(base_url()."admin/list-author.html"); 
        }
       
        $this->ci_smarty->view('backend/update-author.tpl',$this->data); 
    }

    public function deleteAuthor($author_id)
    {
        $this->Mauthor->deteleAuthor($author_id); 
        redirect(base_url()."admin/list-author.html"); 
    }
}